package com.psbc.task.service;

import com.psbc.task.dto.Score;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @author:y022 25/06/2022 13:00
 * @description:
 */
@Service
@Slf4j
public class QueryService {

    @Autowired
    private QueryMapper queryMapper;

    private static final String BAD = "BAD";
    private static final String BAEXCELLENT = "EXCELLENT";
    private static final ExecutorService ex = Executors.newCachedThreadPool();
    private static final CompletionService<List<Score>> c =new ExecutorCompletionService<>(ex);

    public void getList() throws ExecutionException, InterruptedException {
        List<Long> randomList = getRandomList();
        long start = System.currentTimeMillis();
//        Future<List<Score>> res1 = ex.submit(new QueryTask(queryMapper, randomList));
//        Future<List<Score>> res2 = ex.submit(new QueryTask(queryMapper, randomList));
        c.submit(new QueryTask(queryMapper,randomList));
        c.submit(new QueryTask(queryMapper,randomList));
        List<Score> list = c.poll().get();
        List<Score> list1 = c.poll().get();
        List<Score> list2= c.poll().get();
        List<Score> list3= c.poll().get();
        log.debug("res1大小:{}.res2大小:{},res3大小:{},耗时：{} ms", list.size(), list1.size(),list2.size(), System.currentTimeMillis() - start);
    }

    List<Long> getRandomList() {
        Random random = new Random();
        return random.longs(200, 200, 20000).distinct().mapToObj(e -> e).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        Object o = new Object();


        synchronized (o){
            System.out.println(11);
            synchronized (o){
                System.out.println(22);
            }
        }
    }
}
